<?php
     /** 
     * Matches HTML tags in a string and their attributes. 
     * 
     * Returns array in the format: 
     * Array ([0] => Array ([text] => "text in between tags" 
     *                      [attributes] => Array ([attribute1] => [value], 
     *                                             [attribute2] => [value]))); 
     * @param $text string The string to find tags in. 
     * @param $tagname string The tag name of he tag to match e.g: form 
     * @param $clostag boolean Does the tag have corresponding close tags. i.e: </$tagname> 
     * @param $close_optional If a closetag may be missing, it is closed implicitly when another tag of the same name is found. 
     *                        include the name of an additional tags that closes the tag implicitly, here. 
     * @return array An indexed array, one element for each tag. 
     * 
     * @author Adam Delves <codedv @ sccode . com >      
     */ 
    function get_html_tags($text, $tagname, $closetag=true, $close_optional=false) 
    { 
        /* escape PCRE characters in tag name */ 
        $tagname = preg_quote($tagname); 
         
        $ret = array(); 

        /* regular expression to match mattributes in a tag name */ 
        //$attrib_match = "/((?i)[a-z]+) (\s*=\s* ( ((?U)(\"(.*)\")) |(\S+) ) |\s+) /sXi"; 
		$attrib_match = "/((?i)[a-z]+)(\s*=\s*(((?U)(\"(.*)\"))|(\s+))|\s+)/si";
         
        if ($closetag) { 
            if ($close_optional !== false) { 
                $close_optional = preg_quote($close_optional); 
                $regex = "/<$tagname(.*)>(.*)(<\/$tagname>|(?=<$tagname>)|(?=<\/$close_optional>))/Uis"; 
            } else { 
                $regex = "/<$tagname(.*)>(.*)<\/$tagname>/Uis"; 
            } 
        } else { 
            $regex = "/<$tagname((.+))\/?>/Uis"; 
        } 
         
        // regex now matches the tag appropriatly 
         
        preg_match_all($regex, $text, $tags, PREG_SET_ORDER); 

        $tag_count = count($tags); 
         
        for($t = 0; $t < $tag_count; $t++) { 
            $tag = array(); 
             
            /* get attributes */ 
            preg_match_all($attrib_match, $tags[$t][1], $attributes, PREG_SET_ORDER); 

            $attribs = array(); 

            $attrib_count = count($attributes); 
            for($a = 0; $a < $attrib_count; $a++) {     
                $name = strtolower(trim($attributes[$a][1])); 

                if (isset($attributes[$a][4])) { // name value pair found 
                    $value = trim(isset($attributes[$a][7])?$attributes[$a][7]:$attributes[$a][6]); 
                } else { 
                    $value = $name; 
                } 

                $attribs[$name] = $value;                 
            } 
             
            $tag['attributes'] = $attribs; 
             
            if ($closetag) { 
                $tag['text'] = $tags[$t][2]; 
            } 

            $ret[] = $tag; 
        } 

        return $ret; 
    }  
 

 //Test
echo "<pre>";
$htmlCode = file_get_contents("http://www.ourwebstudio.com/");
$search = array ('@<script[^>]*?>.*?</script>@si'); // Strip out javascript             
$replace = array ('');
$htmlCode = preg_replace($search, $replace, $htmlCode);

$anchors = get_html_tags($htmlCode, 'a'); 
echo count($anchors)."\n\n";
// loop through and find anchors with rel="nofollow" attribute 
foreach($anchors as $anchor) { 
    // link text, note: this may contain HTML 
    $text =  $anchor['text']; 

    // look for rel in attributes array 
    //if (array_key_exists('rel', $anchor['attirbutes']) &&  $anchor['attributes']['rel'] == 'nofollow') { 
        /* do stuff here */ 
    //}
	print_r($anchor);
}
?>